package com.diyunapp.xilinapp.me;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.adapters.PopBankAdapter;
import com.diyunapp.xilinapp.bean.Bank;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GetMoneyBankActivity extends StatusBarFragmentActivity {

    @Bind(R.id.recharge_record_tv)
    TextView rechargeRecordTv;
    @Bind(R.id.account_money)
    TextView accountMoney;
    @Bind(R.id.recharge_money_et)
    EditText rechargeMoneyEt;
    @Bind(R.id.bank_name)
    TextView bankName;
    @Bind(R.id.bank_num_edt)
    EditText bankNumEdt;
    @Bind(R.id.bank_username_edt)
    EditText bankUsernameEdt;
    @Bind(R.id.payment_pass_edt)
    EditText paymentPassEdt;
    @Bind(R.id.sure_to_recharge)
    TextView sureToRecharge;
    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.lin_bank_name)
    LinearLayout LinBankName;
    private ListView banksLv;
    private View chooseProvinceDialog;
    private String username;
    private String member_id;
    private String accountMoney1;
    private View headView;
    private View express_parent_view;
    private PopBankAdapter bankAdapter;
    private ListView bankListView;
    private List<Bank> bankStrList;
    private int selected_bank = -1;
    private PopupWindow bankPopuWindow;
    private List<Bank> list_city = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_money_bank);
        ButterKnife.bind(this);
        headView = findViewById(R.id.gmb_menubar_inc);
        super.MenuWidget(headView);
        accountMoney1=getIntent().getExtras().getString("accountMoney");
        accountMoney.setText(accountMoney1);
        username= MainActivity.myLogin.getUsername();
        member_id=MainActivity.myLogin.getMemberid();
        bankPopupWindow();
        initDiaData();
    }
    private void bankPopupWindow() {
        //设置菜单中更多功能使用的PopupWindow
        express_parent_view = (View)findViewById(R.id.dropdown_bank_view);
        LinearLayout root = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.poplistlayout, null);
        bankListView = (ListView)root.findViewById(R.id.pop_list_view);
        bankStrList = new ArrayList<Bank>();
        bankAdapter = new PopBankAdapter(this, bankStrList);
        bankListView.setAdapter(bankAdapter);
        bankListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                selected_bank = position;
                bankName.setText(bankStrList.get(position).getBank_name());

                bankPopuWindow.dismiss();
            }
        });

        bankPopuWindow = new PopupWindow(root,400, LinearLayout.LayoutParams.WRAP_CONTENT);
        // 点击外部可关闭窗口
        bankPopuWindow.setBackgroundDrawable(getDrawable());
        bankPopuWindow.setOutsideTouchable(true);
        bankPopuWindow.setFocusable(true);
    }

    private Drawable getDrawable() {
        ShapeDrawable bgdrawable =new ShapeDrawable(new OvalShape());
        bgdrawable.getPaint().setColor(getResources().getColor(android.R.color.transparent));
        return bgdrawable;
    }
    @OnClick({R.id.recharge_record_tv,R.id.bank_name,R.id.sure_to_recharge,R.id.go_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.recharge_record_tv:
                nextAct(GetMoneyRecordActivity.class);
                break;
            case R.id.bank_name:
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(bankName.getWindowToken(),0);
                bankPopuWindow.showAsDropDown(express_parent_view);
                break;
            case R.id.sure_to_recharge:
                    getMoney();
                break;
            case R.id.go_back:
                finish();
                break;
        }
    }
    private void getMoney() {
        String pdc_amount=rechargeMoneyEt.getText().toString().trim();
        String pdc_bank_name=bankName.getText().toString().trim();
        String pdc_bank_no=bankNumEdt.getText().toString().trim();
        String pdc_bank_user=bankUsernameEdt.getText().toString().trim();
        String password= paymentPassEdt.getText().toString().trim();
        String url=""+MainActivity.url+"act=goods&op=withdrawals" +
                "&member_id="+member_id+"&member_name="+username+"&pdc_amount="+pdc_amount
                +"&pdc_bank_name="+pdc_bank_name+"pdc_bank_no="+pdc_bank_no+"&pdc_bank_user="
                +pdc_bank_user+"&password="+password+"";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);

        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {}
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
                            String data=resultObject.optString("data");
                            if (result==0){
                                toast(data);
                            }else {
                                toast(data);}

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();}
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }
    public void nextAct(Class act){
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }

    private void initDiaData() {
        String url=""+MainActivity.url+"act=goods&op=bank";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.GET, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.getInt("code");
                            if(result==200){
                                JSONArray data=resultObject.optJSONArray("data");
                                for (int i=0;i<data.length();i++){
                                    JSONObject provinceJson = data.getJSONObject(i);
//                                    String id = provinceJson.getString("id");
                                    String name = provinceJson.getString("bank_name");
                                    bankStrList.add(new Bank(name));
                                }
                                bankAdapter.notifyDataSetChanged();

                            }

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }
                    }
                    @Override
                    public void onFailure(HttpException error, String msg) {

                    }
                });
    }
}
